import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import UserList from '../components/admin/UserList'
import UpdPower from '../components/admin/UpdPower'
import UserSportModule from '../components/admin/UserSportModule'
import UserShopModule from '../components/admin/UserShopModule'
import SportsMode from '../components/sport/SportsMode'
import Calorie from '../components/sport/Calorie'
import Health from '../components/sport/Health'

Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    redirect:"/login"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/home",
    component: Home,
      redirect: "welcome",//重定向到welcome页面
      children:[
          {path:"/home",component: Welcome,},
          {path:"/user",component: UserList,},
      ]
  },
    //修改权限
    {
        path: "/home",
        component: Home,
        redirect: "welcome",//重定向到welcome页面
        children:[
            {path:"/home",component: Welcome},
            {path:"/rights",component: UpdPower},
        ]
    },
    //运动模块
    {
        path: "/home",
        component: Home,
        redirect: "welcome",//重定向到welcome页面
        children:[
            {path:"/home",component: Welcome},
            {path:"/sport",component: UserSportModule},
        ]
    },
    //商品模块
    {
        path: "/home",
        component: Home,
        redirect: "welcome",//重定向到welcome页面
        children:[
            {path:"/home",component: Welcome},
            {path:"/goods",component: UserShopModule},
        ]
    },
    //运动科普
    {
        path: "/home",
        component: Home,
        redirect: "welcome",//重定向到welcome页面
        children:[
            {path:"/home",component: Welcome},
            {path:"/Introduction",component: SportsMode},
        ]
    },
    //卡路里
    {
        path: "/home",
        component: Home,
        redirect: "welcome",//重定向到welcome页面
        children:[
            {path:"/home",component: Welcome},
            {path:"/calories",component: Calorie},
        ]
    },
    //营养配餐
    {
        path: "/home",
        component: Home,
        redirect: "welcome",//重定向到welcome页面
        children:[
            {path:"/home",component: Welcome},
            {path:"/food",component: Health},
        ]
    },

]

const router = new VueRouter({
  routes
})
//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
 //to 将要访问
 //from 从哪访问
 //next 接着干 next（url） 重定向到url上 next（）继续访问to路径
 if (to.path=='/login')return next();
 //获取user
  const userFlag = window.sessionStorage.getItem("user");//去除当前用户
  if (!userFlag)return next('/login');//无值，返回登录页
  next();//符合要求 放行
})

export default router
